<template>
  <div>
    <search-hander @handleSearch="handleSearch">
      <template>
        <span>区域搜索：</span>
      </template>
    </search-hander>
    <div class="main">
      <div class="btnBox">
        <el-button class="btn" @click="addDevice">
          <i class="el-icon-circle-plus-outline" />
          新建
        </el-button>
      </div>
      <el-table :data="list" style="width: 100%">
        <el-table-column label="序号" width="70">
          <template v-slot="{$index}">
            {{ ($index + 1) + (params.pageIndex - 1) * 10 }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="区域名称" />
        <el-table-column prop="nodeCount" label="点位数" />
        <el-table-column prop="remark" label="备注说明" />
        <el-table-column fixed="right" label="操作" width="180">
          <template v-slot="{row}">
            <el-button type="text" @click="seeDetail(row.id)">查看详情</el-button>
            <el-button type="text" @click="putRegion(row)">修改</el-button>
            <el-button type="text" @click="deleteRegion(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row v-if="totalPage > 1" align="middle" justify="space-between" style="height: 60px" type="flex">
        <span style="color: #dbdfe5">共{{ totalCount }}条记录  第 {{ params.pageIndex }}/{{ totalPage }} 页</span>
        <el-pagination
          :current-page="params.pageIndex"
          :page-size="params.pageSize"
          :total="+totalCount"
          layout="prev, next"
          next-text="下一页"
          prev-text="上一页"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <el-dialog title="新建区域" :visible.sync="showAddDialog" width="30%" @close="close">
      <el-form label-position="top">
        <el-form-item label="区域名称">
          <el-input v-model="form.regionName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="备注说明">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注（不超过40字）" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showAddDialog = false">取消</el-button>
        <el-button type="primary" @click="addBtn">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改区域" :visible.sync="showPutDialog" width="30%" @close="close">
      <el-form label-position="top">
        <el-form-item label="区域名称">
          <el-input v-model="form.regionName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="备注说明">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注（不超过40字）" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showAddDialog = false">取消</el-button>
        <el-button type="primary" @click="putBtn">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="区域详情" :visible.sync="showDetailDialog" width="30%" @close="close">
      <p>区域名称：<span>6</span></p>
      <span>包含点位：</span>
      <el-table empty-text="暂无数据" :data="detailList">
        <el-table-column label="序号">
          <template v-slot="{$index}">
            {{ ($index + 1) + (params.pageIndex - 1) * 10 }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="点位名称" />
        <el-table-column prop="vmCount" label="设备数量" />
      </el-table>

    </el-dialog>
  </div>
</template>

<script>
import SearchHander from '@/components/SearchHander'
import { getNodeList, deleteRegion, getRegionList, addRegion, putRegion } from '@/api/region'
export default {
  components: {
    SearchHander
  },
  data() {
    return {
      showDetailDialog: false,
      showPutDialog: false,
      form: {
        regionName: null,
        remark: null
      },
      detailList: [],
      list: [],
      showAddDialog: false,
      totalCount: 0,
      totalPage: 0,
      params: {
        pageIndex: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getRegion()
  },
  methods: {
    async seeDetail(id) {
      this.showDetailDialog = true
      this.params.regionId = id
      const res = await getNodeList(this.params)
      this.detailList = res.currentPageRecords
      console.log(this.detailList)
    },
    async deleteRegion(id) {
      const res = await deleteRegion(id)
      if (res) {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getRegion()
      }
    },
    async putBtn() {
      const res = await putRegion(this.form)
      if (res) {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.form = {
          regionName: null,
          remark: null
        }
        this.showPutDialog = false
        this.getRegion()
      }
    },
    close() {
      this.showAddDialog = false
      this.showPutDialog = false
      this.form = {
        regionName: null,
        remark: null
      }
      this.form = {
        regionName: null,
        remark: null
      }
    },
    async putRegion(row) {
      console.log(row)
      this.form = {
        id: row.id,
        regionName: row.name,
        remark: row.remark
      }
      this.showPutDialog = true
    },
    async addBtn() {
      const res = await addRegion(this.form)
      if (res) {
        this.$message({
          message: '添加成功',
          type: 'success'
        })
        this.form = {
          regionName: null,
          remark: null
        }
        this.showAddDialog = false
        this.getRegion()
      }
    },
    async getRegion() {
      const res = await getRegionList(this.params)
      this.list = res.currentPageRecords
      this.totalCount = res.totalCount
      this.totalPage = res.totalPage
      console.log(this.list)
    },
    addDevice() {
      this.showAddDialog = true
    },
    handleSearch(searchText) {
      this.params.pageIndex = 1
      this.params.name = searchText
      this.getRegion(this.params)
    },
    changePage(page) {
      this.params.pageIndex = page
      this.getRegion(this.params)
    }
  }
}
</script>

<style scoped>
.main{
  width: 100%;
  height: 693px;
  padding: 20px;
  background-color: #fff;
  margin-bottom: 70px;
}
.btnBox {
  display: flex;
  padding-bottom: 20px;
}
.btn {
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  width: 80px ;
  height: 36px;
  padding: 0;
  background: linear-gradient(135deg, #ff9743, #ff5e20) ;
  border: none;
  color: white;
}
::v-deep .el-pagination button {
width: 70px;
height: 32px;
margin: 0 16px;
border-radius: 2px;
background-color: #d5ddf8;
}
::v-deep .btn-prev:disabled {
background: #edf0f9;
}
::v-deep .el-table td, .el-table th {
  padding: 10px 0;
}
::v-deep .el-table td, .el-table th.is-leaf{
  border-bottom: 0;
}
</style>
